import 'package:easy_sidemenu/easy_sidemenu.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import '../routes/router_path.dart';

class SideMenuView extends StatefulWidget {
  const SideMenuView({super.key});

  @override
  State<SideMenuView> createState() => _SideMenuViewState();
}

class _SideMenuViewState extends State<SideMenuView> {
  SideMenuController sideMenu = SideMenuController();

  @override
  void initState() {
    sideMenu.addListener((index) {
      //context.go("/user");
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SideMenu(
      controller: sideMenu,
      style: SideMenuStyle(
        // showTooltip: false,
        displayMode: SideMenuDisplayMode.auto,
        showHamburger: true,
        hoverColor: Colors.blue[100],
        selectedHoverColor: Colors.blue[100],
        selectedColor: Colors.lightBlue,
        selectedTitleTextStyle: const TextStyle(color: Colors.white),
        selectedIconColor: Colors.white,
        selectedTitleTextStyleExpandable: const TextStyle(
          color: Colors.lightBlue,
        ),
        // decoration: BoxDecoration(
        //   borderRadius: BorderRadius.all(Radius.circular(10)),
        // ),
        // backgroundColor: Colors.grey[200]
      ),
      title: Column(
        children: [
          ConstrainedBox(
            constraints: const BoxConstraints(maxHeight: 150, maxWidth: 150),
            child: Image.asset('assets/images/easy_sidemenu.png'),
          ),
          const Divider(indent: 8.0, endIndent: 8.0),
        ],
      ),
      footer: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Container(
          decoration: BoxDecoration(
            color: Colors.lightBlue[50],
            borderRadius: BorderRadius.circular(12),
          ),
          child: Padding(
            padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 10),
            child: Text(
              'michael',
              style: TextStyle(fontSize: 15, color: Colors.grey[800]),
            ),
          ),
        ),
      ),
      items: <SideMenuItemType>[
        SideMenuItem(
          title: 'Dashboard',
          onTap: (index, _) {
            sideMenu.changePage(index);
            replaceLocation(Paths.DASHBOARD);
          },
          icon: const Icon(Icons.home),
          badgeContent: const Text('3', style: TextStyle(color: Colors.white)),
          tooltipContent: "This is a tooltip for Dashboard item",
        ),
        SideMenuExpansionItem(
          title: "内容管理",
          icon: const Icon(Icons.content_copy_outlined),
          onTap: (index, _, isExpanded) => {},
          children: [
            SideMenuItem(
              title: '已发布列表',
              onTap: (index, _) {
                sideMenu.changePage(index);
                replaceLocation(Paths.CONTENT_PUBLISH);
              },
              icon: const Icon(Icons.publish_outlined),
            ),
            SideMenuItem(
              title: '草稿箱',
              onTap: (index, _) {
                sideMenu.changePage(index);
                replaceLocation(Paths.CONTENT_DRAFT);
              },
              icon: const Icon(Icons.supervisor_account),
            ),
          ],
        ),
        SideMenuExpansionItem(
          title: "用户管理",
          icon: const Icon(Icons.kitchen),
          onTap: (index, _, isExpanded) => {},
          children: [
            SideMenuItem(
              title: '用户列表',
              onTap: (index, _) {
                sideMenu.changePage(index);
                replaceLocation(Paths.USER);
              },
              icon: const Icon(Icons.person),
            ),
          ],
        ),
        SideMenuItem(
          builder: (context, displayMode) {
            return const Divider(endIndent: 8, indent: 8);
          },
        ),
        SideMenuItem(
          title: '设置',
          onTap: (index, _) {
            sideMenu.changePage(index);
          },
          icon: const Icon(Icons.settings),
        ),
      ],
    );
  }


  goLocation(String path) {
    context.go(path);
  }

  replaceLocation(String path) {
    context.replace(path);
  }

}
